package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.services.business.dto.response.StatisticsAssetOrderDto;
import com.molichuxing.services.infrastructure.bean.StatisticsAssetBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.StatisticsAssetMapper;
import com.molichuxing.services.infrastructure.dto.request.create.StatisticsAssetCreateDto;
import com.molichuxing.services.infrastructure.dto.response.StatisticsAssetDto;
import com.molichuxing.services.infrastructure.service.StatisticsAssetService;
import com.molichuxing.services.property.StatisticsAssetInStockTypeEnum;
import com.molichuxing.services.property.StatisticsAssetRentalTypeEnum;
import com.molichuxing.services.property.StatisticsAssetRollbackOverdueTypeEnum;
import com.molichuxing.services.property.StatisticsAssetUseTypeEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 资产统计
 * </p>
 *
 * @author zhaopenghui
 * @since 2020-08-25
 */
@Service("statisticsAssetService")
public class StatisticsAssetServiceImpl implements StatisticsAssetService {

    private static final Logger log = Logger.getLogger(StatisticsAssetServiceImpl.class);

    @Resource
    private StatisticsAssetMapper statisticsAssetMapper;


    @Override
    public Map<Integer, Integer> getStatisticsAssetCount(LocalDateTime dateTime) {
        Map<Integer, Integer> returnMap = new HashMap<Integer, Integer>();
        LocalDateTime startTime = DateUtil.getDayStart(DateUtil.firstDayOfMonth(dateTime));
        LocalDateTime endTime = DateUtil.getDayEnd(DateUtil.lastDayOfMonth(dateTime));
        List<TabDto> countByUseType = statisticsAssetMapper.getCountByUseType(startTime, endTime);
        if (null == countByUseType || countByUseType.size() == 0) {
            return returnMap;
        }
        Map<Integer, Integer> countByUseTypeMap = countByUseType.stream().collect(Collectors.toMap(TabDto::getType, TabDto::getCount));
        for (StatisticsAssetUseTypeEnum typeEnum : StatisticsAssetUseTypeEnum.values()) {
            int value = typeEnum.getValue();
            Integer count = countByUseTypeMap.get(value);
            if (null == count) {
                count = 0;
            }
            returnMap.put(value, count);
        }

        List<TabDto> countByRentalType = statisticsAssetMapper.getCountByRentalType(startTime, endTime);
        if (null == countByRentalType || countByRentalType.size() == 0) {
            return returnMap;
        }
        Map<Integer, Integer> countByRentalTypeMap = countByRentalType.stream().collect(Collectors.toMap(TabDto::getType, TabDto::getCount));
        for (StatisticsAssetRentalTypeEnum typeEnum : StatisticsAssetRentalTypeEnum.values()) {
            int value = typeEnum.getValue();
            Integer count = countByRentalTypeMap.get(value);
            if (null == count) {
                count = 0;
            }
            returnMap.put(value, count);
        }

        List<TabDto> countByRollbackOverdueType = statisticsAssetMapper.getCountByRollbackOverdueType(startTime, endTime);
        if (null == countByRollbackOverdueType || countByRollbackOverdueType.size() == 0) {
            return returnMap;
        }
        Map<Integer, Integer> countByRollbackOverdueTypeMap = countByRollbackOverdueType.stream().collect(Collectors.toMap(TabDto::getType, TabDto::getCount));
        for (StatisticsAssetRollbackOverdueTypeEnum typeEnum : StatisticsAssetRollbackOverdueTypeEnum.values()) {
            int value = typeEnum.getValue();
            Integer count = countByRollbackOverdueTypeMap.get(value);
            if (null == count) {
                count = 0;
            }
            returnMap.put(value, count);
        }

        List<TabDto> countByInStockType = statisticsAssetMapper.getCountByInStockType(startTime, endTime);
        if (null == countByInStockType || countByInStockType.size() == 0) {
            return returnMap;
        }
        Map<Integer, Integer> countByInStockTypeMap = countByInStockType.stream().collect(Collectors.toMap(TabDto::getType, TabDto::getCount));
        for (StatisticsAssetInStockTypeEnum typeEnum : StatisticsAssetInStockTypeEnum.values()) {
            int value = typeEnum.getValue();
            Integer count = countByInStockTypeMap.get(value);
            if (null == count) {
                count = 0;
            }
            returnMap.put(value, count);
        }
        return returnMap;
    }

    @Override
    public List<StatisticsAssetDto> getStatisticsAssetList(LocalDateTime dateTime) {
        LocalDateTime startTime = DateUtil.getDayStart(DateUtil.firstDayOfMonth(dateTime));
        LocalDateTime endTime = DateUtil.getDayEnd(DateUtil.lastDayOfMonth(dateTime));
        List<StatisticsAssetBean> list = statisticsAssetMapper.getList(startTime, endTime);
        return Convert.toStatisticsAssetDtoList(list);
    }

    @Override
    public Boolean deleteAsset() {
        statisticsAssetMapper.deleteAsset();
        return true;
    }

    @Override
    public List<StatisticsAssetOrderDto> getScrappedCarStatisticsAssetOrder() {
        return statisticsAssetMapper.getScrappedCarStatisticsAssetOrder();
    }

    @Override
    public List<StatisticsAssetOrderDto> getLostCarStatisticsAssetOrder() {
        return statisticsAssetMapper.getLostCarStatisticsAssetOrder();
    }

    @Override
    public List<StatisticsAssetOrderDto> getWaitingCarStatisticsAssetOrderDto() {
        return statisticsAssetMapper.getWaitingCarStatisticsAssetOrderDto();
    }

    @Override
    public List<StatisticsAssetOrderDto> getLawsuitCarStatisticsAssetOrderDto() {
        return statisticsAssetMapper.getLawsuitCarStatisticsAssetOrderDto();
    }

    @Override
    public List<StatisticsAssetOrderDto> getBatchSaleCarStatisticsAssetOrderDto() {
        return statisticsAssetMapper.getBatchSaleCarStatisticsAssetOrderDto();
    }

    @Override
    public List<StatisticsAssetOrderDto> getInCarStoreStatisticsAssetOrderDto() {
        return statisticsAssetMapper.getInCarStoreStatisticsAssetOrderDto();
    }

    @Override
    public List<StatisticsAssetOrderDto> geOfficialCarStatisticsAssetOrderDto() {
        return statisticsAssetMapper.geOfficialCarStatisticsAssetOrderDto();
    }

    @Override
    public void saveBatchStatisticsAsset(List<StatisticsAssetCreateDto> dtoList) {
        List<StatisticsAssetBean> beanList = Convert.toStatisticsAssetBeanList(dtoList);
        if (!CollectionUtils.isEmpty(beanList)){
            statisticsAssetMapper.saveBatchStatisticsAsset(beanList);
        }
    }
}
